package TransactionImplementation

//发薪日事务
import (
	"SalaryPayment/DataBase"
	"SalaryPayment/Model"
	"time"
)

//结构体
type PaydayTransaction struct {
	itsPayDate  time.Time
	itsPayCheck map[int]Model.Salarycheck
}

//发薪水操作
func (pt *PaydayTransaction) Execute() {
	ids := DataBase.PayrollDatabase.GetAllEmployee()

	for _, id := range ids {
		e := DataBase.PayrollDatabase.GetEmployee(id)

		if e != nil {
			if e.IsPayDate(pt.itsPayDate) {
				pc := Model.NewSalarycheck(e.GetSalaryStartDate(pt.itsPayDate), pt.itsPayDate)
				pt.itsPayCheck[id] = pc
				e.Payday(pc)
			}
		}
	}
}

func (pt *PaydayTransaction) GetPaycheck(empid int) Model.Salarycheck {
	if pc, ok := pt.itsPayCheck[empid]; ok {
		return pc
	}

	return Model.Salarycheck{}
}

func (pt *PaydayTransaction) GetPaycheckCount() int {
	return len(pt.itsPayCheck)
}
